#include "Mesh.h"

Mesh::Mesh(sgm::Mesh *mesh, CGparameter matrixParam)
{
	this ->mesh = mesh;
	this ->matrixParam = matrixParam;
	material = NULL;

	glGenBuffersARB(1, &vboId);
	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);
	glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh ->verticesCount * sizeof(sgm::Verticle),
		mesh ->vertices, GL_STATIC_DRAW_ARB);

	glVertexPointer(3, GL_FLOAT, sizeof(sgm::Verticle), 0);
	glNormalPointer(GL_FLOAT, sizeof(sgm::Verticle), (void*)sizeof(Vec3));
	glTexCoordPointer(2, GL_FLOAT, sizeof(sgm::Verticle), (void*)(sizeof(Vec3) * 2));
}

void Mesh::Process(float ms)
{
}

void Mesh::Render()
{
	if (material != NULL)
		material ->SetMaterial();
	else
	{
		//cgGLDisableProfile(CG_PROFILE_VP30);
		//cgGLDisableProfile(CG_PROFILE_FP30);
	}

	if (matrixParam != NULL)
	{
		//cgGLEnableProfile(CG_PROFILE_VP30);
		//cgGLEnableProfile(CG_PROFILE_FP30);
		cgGLSetStateMatrixParameter(matrixParam, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
	}

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);

	glVertexPointer(3, GL_FLOAT, sizeof(sgm::Verticle), 0);
	glNormalPointer(GL_FLOAT, sizeof(sgm::Verticle), (void*)sizeof(Vec3));
	glTexCoordPointer(2, GL_FLOAT, sizeof(sgm::Verticle), (void*)(sizeof(Vec3) * 2));

	glDrawArrays(GL_TRIANGLES, 0, mesh ->verticesCount);

	glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	
	/*glBegin(GL_TRIANGLES);
	for (int i = 0; i < mesh ->verticesCount; i++)
	{
		glNormal3f(mesh ->vertices[i].normal.x, mesh ->vertices[i].normal.y, mesh ->vertices[i].normal.z);
		glTexCoord2f(mesh ->vertices[i].texCoord.u, mesh ->vertices[i].texCoord.v);
		glVertex3f(mesh ->vertices[i].position.x, mesh ->vertices[i].position.y, mesh ->vertices[i].position.z);
	}
	glEnd();*/
}

void Mesh::SetMaterial(Material *material)
{
	this ->material = material;
}

void Mesh::SetPosition(sm::Vec3 pos)
{
}

void Mesh::SetRotation(float angle, sm::Vec3 axis)
{
}

void Mesh::SetScale(sm::Vec3 scale)
{
}

void Mesh::SetRoll(float angle)
{
}

int Mesh::GetId()
{
	return mesh ->id;
}